//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;

namespace OpenLS.Chart
{
    public class ChartStyle : IOfficeXmlSerializable
    {
        private int _index;

        public ChartStyle(): this(2)
        {
        }

        public ChartStyle(int index)
        {
            if (index < 1 || index > 48)
                throw new ArgumentException("index");
            _index = index;
        }

        public ColorBase AxisColor
        {
            get
            {
                if (Index <= 32)
                    return new ThemeColor(SchemeColorValue.Text1);
                return new ThemeColor(SchemeColorValue.Dark1);
            }
        }

        public ColorBase MajorGridLineColor
        {
            get { return AxisColor; }
        }

        public ColorBase MinorGridLineColor
        {
            get { return new ThemeColor(SchemeColorValue.Text1, 0.5); }
        }

        public ColorBase ChartAreaFillColor
        {
            get
            {
                if (Index <= 32)
                    return new ThemeColor(SchemeColorValue.Background1);
                if (Index <= 40)
                    return new ThemeColor(SchemeColorValue.Light1);
                if (Index <= 48)
                    return new ThemeColor(SchemeColorValue.Dark1);
                throw new NotSupportedException();
            }
        }
        public ColorBase FloorColor { get { return ChartAreaFillColor; } }

        public ColorBase PlotAreaColor
        {
            get
            {
                if (Index <= 32)
                    return new ThemeColor(SchemeColorValue.Background1);
                switch (Index)
                {
                    case 33:
                    case 34:
                        return new ThemeColor(SchemeColorValue.Dark1, 0.2);
                    case 35:
                        return new ThemeColor(SchemeColorValue.Accent1, 0.2);
                    case 36:
                        return new ThemeColor(SchemeColorValue.Accent2, 0.2);
                    case 37:
                        return new ThemeColor(SchemeColorValue.Accent3, 0.2);
                    case 38:
                        return new ThemeColor(SchemeColorValue.Accent4, 0.2);
                    case 39:
                        return new ThemeColor(SchemeColorValue.Accent5, 0.2);
                    case 40:
                        return new ThemeColor(SchemeColorValue.Accent6, 0.2);
                }
                return new ThemeColor(SchemeColorValue.Dark1, 0.95);
            }
        }

        public int Index
        {
            get { return _index; }
        }

        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext ctx = context.Read("style"))
            {
                _index = ctx.GetInteger("val");
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (WriteContext ctx = context.Write("style"))
            {
                ctx.SetInteger("val", Index);
            }
        }

        #endregion
    }
}